﻿using System;
using UnityEngine;

// Token: 0x0200012B RID: 299
[AddComponentMenu("RVP/C#/Stunt/Flip Control", 2)]
[DisallowMultipleComponent]
[RequireComponent(typeof(VehicleParent))]
public class FlipControl : MonoBehaviour
{
	// Token: 0x0600057A RID: 1402 RVA: 0x0002A498 File Offset: 0x00028698
	private void Start()
	{
		this.tr = base.transform;
		this.rb = base.GetComponent<Rigidbody>();
		this.vp = base.GetComponent<VehicleParent>();
	}

	// Token: 0x0600057B RID: 1403 RVA: 0x0002A4C0 File Offset: 0x000286C0
	private void FixedUpdate()
	{
		if (this.vp.groundedWheels == 0 && (!this.vp.crashing || (this.vp.crashing && !this.disableDuringCrash)))
		{
			this.velDir = Quaternion.LookRotation(GlobalControl.worldUpDir, this.rb.velocity);
			if (this.flipPower != Vector3.zero)
			{
				this.ApplyFlip();
			}
			if (this.stopFlip)
			{
				this.ApplyStopFlip();
			}
			if (this.rotationCorrection != Vector3.zero)
			{
				this.ApplyRotationCorrection();
			}
			if (this.diveFactor > 0f)
			{
				this.Dive();
			}
		}
	}

	// Token: 0x0600057C RID: 1404 RVA: 0x0002A580 File Offset: 0x00028780
	private void ApplyFlip()
	{
		Vector3 torque;
		if (this.freeSpinFlip)
		{
			torque = new Vector3(this.vp.pitchInput * this.flipPower.x, this.vp.yawInput * this.flipPower.y, this.vp.rollInput * this.flipPower.z);
		}
		else
		{
			torque = new Vector3((this.vp.pitchInput == 0f || Mathf.Abs(this.vp.localAngularVel.x) <= 1f || Math.Sign(this.vp.pitchInput * Mathf.Sign(this.flipPower.x)) == Math.Sign(this.vp.localAngularVel.x)) ? (this.vp.pitchInput * this.flipPower.x - this.vp.localAngularVel.x * (1f - Mathf.Abs(this.vp.pitchInput)) * Mathf.Abs(this.flipPower.x)) : (-this.vp.localAngularVel.x * Mathf.Abs(this.flipPower.x)), (this.vp.yawInput == 0f || Mathf.Abs(this.vp.localAngularVel.y) <= 1f || Math.Sign(this.vp.yawInput * Mathf.Sign(this.flipPower.y)) == Math.Sign(this.vp.localAngularVel.y)) ? (this.vp.yawInput * this.flipPower.y - this.vp.localAngularVel.y * (1f - Mathf.Abs(this.vp.yawInput)) * Mathf.Abs(this.flipPower.y)) : (-this.vp.localAngularVel.y * Mathf.Abs(this.flipPower.y)), (this.vp.rollInput == 0f || Mathf.Abs(this.vp.localAngularVel.z) <= 1f || Math.Sign(this.vp.rollInput * Mathf.Sign(this.flipPower.z)) == Math.Sign(this.vp.localAngularVel.z)) ? (this.vp.rollInput * this.flipPower.z - this.vp.localAngularVel.z * (1f - Mathf.Abs(this.vp.rollInput)) * Mathf.Abs(this.flipPower.z)) : (-this.vp.localAngularVel.z * Mathf.Abs(this.flipPower.z)));
		}
		this.rb.AddRelativeTorque(torque, ForceMode.Acceleration);
	}

	// Token: 0x0600057D RID: 1405 RVA: 0x0002A8B0 File Offset: 0x00028AB0
	private void ApplyStopFlip()
	{
		Vector3 zero = Vector3.zero;
		zero.x = ((this.vp.pitchInput * this.flipPower.x != 0f) ? 0f : (Mathf.Pow(Mathf.Clamp01(this.vp.upDot), Mathf.Clamp(10f - Mathf.Abs(this.vp.localAngularVel.x), 2f, 10f)) * 10f));
		zero.y = ((this.vp.yawInput * this.flipPower.y != 0f || this.vp.sqrVelMag <= 5f) ? 0f : (Mathf.Pow(Mathf.Clamp01(Vector3.Dot(this.vp.forwardDir, this.velDir * Vector3.up)), Mathf.Clamp(10f - Mathf.Abs(this.vp.localAngularVel.y), 2f, 10f)) * 10f));
		zero.z = ((this.vp.rollInput * this.flipPower.z != 0f) ? 0f : (Mathf.Pow(Mathf.Clamp01(this.vp.upDot), Mathf.Clamp(10f - Mathf.Abs(this.vp.localAngularVel.z), 2f, 10f)) * 10f));
		this.rb.AddRelativeTorque(new Vector3(-this.vp.localAngularVel.x * zero.x, -this.vp.localAngularVel.y * zero.y, -this.vp.localAngularVel.z * zero.z), ForceMode.Acceleration);
	}

	// Token: 0x0600057E RID: 1406 RVA: 0x0002AAB0 File Offset: 0x00028CB0
	private void ApplyRotationCorrection()
	{
		float num = this.vp.forwardDot;
		float num2 = this.vp.rightDot;
		float f = this.vp.upDot;
		RaycastHit raycastHit;
		if (this.groundCheckDistance > 0f && Physics.Raycast(this.tr.position, (-GlobalControl.worldUpDir + this.rb.velocity).normalized, out raycastHit, this.groundCheckDistance, GlobalControl.groundMaskStatic) && Vector3.Dot(raycastHit.normal, GlobalControl.worldUpDir) >= this.groundSteepnessLimit)
		{
			num = Vector3.Dot(this.vp.forwardDir, raycastHit.normal);
			num2 = Vector3.Dot(this.vp.rightDir, raycastHit.normal);
			f = Vector3.Dot(this.vp.upDir, raycastHit.normal);
		}
		this.rb.AddRelativeTorque(new Vector3((this.vp.pitchInput * this.flipPower.x != 0f) ? 0f : (num * (1f - Mathf.Abs(num2)) * this.rotationCorrection.x - this.vp.localAngularVel.x * Mathf.Pow(f, 2f) * 10f), (this.vp.yawInput * this.flipPower.y != 0f || this.vp.sqrVelMag <= 10f) ? 0f : (Vector3.Dot(this.vp.forwardDir, this.velDir * Vector3.right) * Mathf.Abs(f) * this.rotationCorrection.y - this.vp.localAngularVel.y * Mathf.Pow(f, 2f) * 10f), (this.vp.rollInput * this.flipPower.z != 0f) ? 0f : (-num2 * (1f - Mathf.Abs(num)) * this.rotationCorrection.z - this.vp.localAngularVel.z * Mathf.Pow(f, 2f) * 10f)), ForceMode.Acceleration);
	}

	// Token: 0x0600057F RID: 1407 RVA: 0x0002AD18 File Offset: 0x00028F18
	private void Dive()
	{
		this.rb.AddTorque(this.velDir * Vector3.left * Mathf.Clamp01(this.vp.velMag * 0.01f) * Mathf.Clamp01(this.vp.upDot) * this.diveFactor, ForceMode.Acceleration);
	}

	// Token: 0x040007AD RID: 1965
	private Transform tr;

	// Token: 0x040007AE RID: 1966
	private Rigidbody rb;

	// Token: 0x040007AF RID: 1967
	private VehicleParent vp;

	// Token: 0x040007B0 RID: 1968
	public bool disableDuringCrash;

	// Token: 0x040007B1 RID: 1969
	public Vector3 flipPower;

	// Token: 0x040007B2 RID: 1970
	[Tooltip("Continue spinning if input is stopped")]
	public bool freeSpinFlip;

	// Token: 0x040007B3 RID: 1971
	[Tooltip("Stop spinning if input is stopped and vehicle is upright")]
	public bool stopFlip;

	// Token: 0x040007B4 RID: 1972
	[Tooltip("How quickly the vehicle will rotate upright in air")]
	public Vector3 rotationCorrection;

	// Token: 0x040007B5 RID: 1973
	private Quaternion velDir;

	// Token: 0x040007B6 RID: 1974
	[Tooltip("Distance to check for ground for reference normal for rotation correction")]
	public float groundCheckDistance = 100f;

	// Token: 0x040007B7 RID: 1975
	[Tooltip("Minimum dot product between ground normal and global up direction for rotation correction")]
	public float groundSteepnessLimit = 0.5f;

	// Token: 0x040007B8 RID: 1976
	[Tooltip("How quickly the vehicle will dive in the direction it's soaring")]
	public float diveFactor;
}
